package com.websocket.endpoint;


import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * @program: RuoYi-Cloud-master
 * @ClassName IntelligentCustomerServiceEndpoint
 * @description:
 * @author:
 * @create: 2023-11-22 20:47
 * @Version 1.0
 **/
@ServerEndpoint("/websocket/customerService/{loginName}")//
@Component
public class IntelligentCustomerServiceEndpoint {
    //静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
    private static final AtomicInteger onlineNum = new AtomicInteger(0);
    //concurrent包的线程安全Set，用来存放每个客户端对应的Session对象。
    public static final Set<IntelligentCustomerServiceEndpoint> connectionsCustomerService = new CopyOnWriteArraySet<>();

    public String loginName;
    public Session session;

    private IntelligentCustomerEndpoint pair = null;//null 空闲的状态
    public IntelligentCustomerEndpoint getPair() {
        return pair;
    }

    public void setPair(IntelligentCustomerEndpoint pair) {
        this.pair = pair;
    }

    /**
     * 当有ws 客户端连接进来，调用的方法
     * @param session
     */
    @OnOpen //创建一个websocket 就会执行OnOpen
    public void start(Session session, @PathParam("loginName") String loginName) throws IOException {
        this.loginName = loginName;
        this.session = session;
        System.out.println("this:" + this);
        connectionsCustomerService.add(this);

        onlineNum.incrementAndGet();//i++  ++i

        String msg = String.format("客服 %s(%s)加入，当前在线人数 %d",loginName,session.toString(),onlineNum.get());
        System.out.println(msg);
        this.session.getBasicRemote().sendText(msg);//给自己发欢迎消息
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose //ws.close
    public void end(Session session,@PathParam("loginName") String loginName) throws IOException {
        connectionsCustomerService.remove(this);
        onlineNum.decrementAndGet();//i--
        String msg = String.format("客服%s(%s) 退出，当前在线人数 %d",loginName,session.toString(),onlineNum.get());
        System.out.println(msg);
        this.session.getBasicRemote().sendText(msg);//给自己发离开的消息
    }

    /**
     * 接收到消息的监听方法
     * @param message
     */
    @OnMessage //ws.send
    public void incoming(Session session, String message, @PathParam("loginName") String loginName) throws IOException {
        String msg = String.format("发送者%s(%s)发送消息： %s",loginName,session.toString(),message);
        System.out.println(msg);

        if(this.pair!=null){//
            this.pair.session.getBasicRemote().sendText(this.loginName +":"+ message);//给对方发
            this.session.getBasicRemote().sendText(this.loginName +":"+ message);//给自己发
        }
    }

    @OnError
    public void onError(Throwable t) throws Throwable {
        System.out.println("onError");
        t.printStackTrace();
    }

}
